/**
 * Created by Fisun on 2016/11/22.
 */

$(function(){
    //获取地址栏信息
    //var str=window.location.search.substr(1)+"";
    //var m=/\d+/.exec(str);
    //var h;
    //m=m[0];

    function GetQueryString(name){
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null) return encodeURI(r[2]);
        return null;
    }
    var flag = GetQueryString("flag");//获取省钱控，点击商品详情的Id
    console.log(flag);

    //获取十大品牌信息
    $.ajax({
        url: URL.getbrand,
        dataType:'json',
        data:{brandtitleid:flag},
        success: function(data) {
            var data=data.result;
            var temp="";
            for(var i=0;i<data.length;i++){
                temp+= '<a href="#" class="nav" id="'+data[i].brandTitleId+'">'
                       +' <div class="f_left fl">'+(i+1)+'</div>'
                       +' <div class="f_left">'
                       +' <p>'+data[i].brandName+'</p>'
                       +' <span>'+data[i].brandInfo+'</span>'
                       +' </div>'
                       +' <span class="glyphicon glyphicon-menu-down sanjiao"></span>'
                       +' </a>'
            }
            $(".ppai").append(temp);
            $(".nav").on('click',function(){
                var id=this.id;
                //获取产品排行信息
                $.ajax({
                    url:URL.getbrandproductlist,
                    dataType:'json',
                    data:{'brandtitleid':id},
                    success: function(data) {
                        var html = template('test', data);

                        $(".brand").html(html);
                        $(".paihang").on("click",function(){
                           // 获取产品评价并渲染到页面
                           var id=this.id;
                            $.ajax({
                                url:URL.getproductcom,
                                data:{"productid":id},
                                dataType:'json',
                                success:function(data){
                                    var html = template('brand2', data);
                                    $(".brand2").html(html);
                                    h=$(".btm").height()*data.result.length;
                                    //console.log(h);
                                    //console.log($("html").height());
                                }
                            });
                            var name=$(this)[0].children[1].innerHTML;
                            $(".tv").html(name);
                            $(".sect").css("display","block");
                            return false;
                        })
                    }
                });
                $("section").addClass("gdu");
                $(".cbian").addClass("hao");

                return false;
            })
        }
    });
    $("#pinl").on("click",function(){
        $("#pinl").parent().parent().parent().css("display","none");
    });
    $(".spa").on("click",function(){
        $("section").removeClass("gdu");
        $(".cbian").removeClass("hao");
    });
    //手指滑动事件
    var startX = 0;
    var moveX = 0;
    /*移动距离*/
    var distanceX = 0;
    /*判断是否滑动过*/
    var isMove = false;
    var brand=$(".brand2")[0];
    /*绑定事件*/
    window.addEventListener('touchstart',function(e){
        startX = e.touches[0].clientX;
    });
    window.addEventListener('touchmove',function(e){
        isMove = true;
        moveX = e.touches[0].clientX;
        distanceX += moveX - startX;
        if(distanceX<=0){
            distanceX=0;
        }
        if(distanceX>=h+150){
            distanceX=h+150;
        }
        setTranslateX(-distanceX);
    });
    window.addEventListener('touchend',function(e){
        /*重置参数  防止第二次的时候影响计算*/
        startX = 0;
        moveX = 0;
        isMove = false;

    });
    var setTranslateX = function(x){
        brand.style.webkitTransform = "translateY("+x+"px)";
        brand.style.transform = "translateY("+x+"px)";
    };
    //点击回到顶部
    $(".ca").on("click", function () {
        var timer =setInterval(function(){
            var leader = document.body.scrollTop;
            var target = 0;
            var step = (target-leader)/10;
            step=step>0?Math.ceil(step):Math.floor(step);
            leader=leader+step;
            if(leader==target){
                clearInterval(timer);
            }
            document.body.scrollTop = leader;
        },30)
        return false;
    })
    //    箭头
    //窗口滚动动态改变火箭位置
    window.onscroll = function(){
        //滚动窗口,动态改变火箭位置
        var arrowDis = document.body.scrollTop;
        //console.log(arrowDis);
        $(".Bcj_returnArrow").css({
            transition:"all 1s",
            position:"absolute",
            right: "20/75rem",
            bottom:""+(-arrowDis)+"px"
        })
    }
//返回顶部火箭
    $(".Bcj_returnArrow").on("click",function(){
        var timer =setInterval(function(){
            var leader = document.body.scrollTop;
            var target = 0;
            var step = (target-leader)/10;
            step=step>0?Math.ceil(step):Math.floor(step);
            leader=leader+step;
            if(leader==target){
                clearInterval(timer);
            }
            document.body.scrollTop = leader;
        },15)
    })

});


